package dao

import (
	"fmt"
	"time"
)

type CarInfo struct {
	UserId    int `json:"user_id"`
	UniqueNum int `json:"unique_num"`
}

type CarInfoResponse struct {
	CarID int `json:"car_id"`
	CarInfo
	GoodId      int       `json:"good_id"`
	Color       string    `json:"color"`
	Parm        string    `json:"parm"`
	Price       float64   `json:"price"`
	Create_time time.Time `json:"create_Time"`
	Num         int       `json:"num"`
	Image       string    `json:"image"`
}

//type CarDetail struct {
//	GoodID string`json:"good_id"`
//	Color string`json:"color"`
//	Parm string`json:"parm"`
//	Price string`json:"price"`
//}

func QueryUniqueDetail(unique string) (gid string, color string, parm string, price string) {

	sqlStr := fmt.Sprintf("select good_idfk, color_fk, parm_fk, price_fk from unique_id where `unique` = %s", unique)
	rows, err := DB.Query(sqlStr)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer rows.Close()
	for rows.Next() {
		err = rows.Scan(&gid, &color, &parm, &price)
		if err != nil {
			fmt.Println(err)
		}
	}
	return
}

func AddCar(carInfo CarInfo) (err error) {
	sqlStr := "CALL func_addcar(?,?)"
	_, err = DB.Exec(sqlStr, carInfo.UserId, carInfo.UniqueNum)
	//fmt.Println(string()str)
	if err != nil {
		fmt.Println("add car err:", err)
	}

	return
}

//func IfTheCarRight(info CarInfo) (ok bool) {
//	ok = false
//	sqlStr := fmt.Sprintf("select a.color, b.ram_rom from phone_color a, phone_parm b where a.good_id = %s && a.color = '%s' && b.ram_rom = '%s'", info.GoodId, info.Color, info.Parm)
//	//sqlStr := "select a.color, b.ram_rom from phone_color a, phone_parm b where a.good_id = ?, a.color = '?' , b.ram_rom = '?'"
//	fmt.Println(sqlStr)
//	rows, err := DB.Query(sqlStr)
//	if err != nil {
//		fmt.Println(err)
//		return false
//	}
//	defer rows.Close()
//	if rows.Next() {
//		var temp CarInfo
//		rows.Scan(&temp.Color, &temp.Parm)
//		ok = true
//	}
//	return
//}

func QueryCar(id string) (res []CarInfoResponse) {
	sqlStr := "SELECT c.id, c.user_id, c.unique_num, c.good_idfk,  c.colorfk, c.parmfk, c.price, c.add_time, c.num, u.image " +
		"FROM car c, unique_id u " +
		"WHERE c.user_id = ? && c.unique_num = u.`unique` && c.status = 1; "
	rows, err := DB.Query(sqlStr, id)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer rows.Close()
	for rows.Next() {
		var temp CarInfoResponse
		rows.Scan(
			&temp.CarID,
			&temp.UserId,
			&temp.UniqueNum,
			&temp.GoodId,
			&temp.Color,
			&temp.Parm,
			&temp.Price,
			&temp.Create_time,
			&temp.Num,
			&temp.Image,
		)
		//fmt.Println(temp.Image)
		fmt.Println(temp)
		res = append(res, temp)
	}
	return
}

func DeleteCar(car_id string, uid int) (row int64, err error) {
	sqlStr := "update car set car.status = 0 where car.id = ? && user_id = ? &&car.status = 1"
	//sqlStr := "delete from car where car.id = ? && user_id = ? &&car.status = 1"
	res, err := DB.Exec(sqlStr, car_id, uid)
	if err != nil {
		fmt.Println(err)
		return
	}
	affected, err := res.RowsAffected()
	if err != nil {
		fmt.Println(err)
	}
	row = affected
	return
}

func UpdateCar(car_id string, num string, uid int) (row int64, err error) {
	sqlStr := "UPDATE  car set `num` = ? where car.id = ? && user_id = ? && car.status = 1"
	res, err := DB.Exec(sqlStr, num, car_id, uid)
	if err != nil {
		fmt.Println(err)
		return
	}
	affected, err := res.RowsAffected()
	if err != nil {
		fmt.Println(err)
	}
	row = affected
	return
}

func CleanCar(uid string) (affected int64, err error) {
	sqlStr := "UPDATE car SET car.`status` = 0 WHERE car.user_id = ?;"
	res, err := DB.Exec(sqlStr, uid)
	if err != nil {
		fmt.Println("CancelCar", err)
		return
	}
	affected, err = res.RowsAffected()
	if err != nil {
		fmt.Println("CancelCar", err)
		return
	}
	fmt.Println("cancel.RowsAffected(),", affected)
	return
}
